+function ($) {

    var requestUrlPrefix = '/';

    var error = function (err) {
        if (err && err.responseJSON) {
            $.tip.error(err.responseJSON.result);
        } else {
            console.log(err);
            $.tip.error('Error !Please Check Console!')
        }
    };

    var getOption = function (options) {
        if (options) {
            var oldUrl = options.url;
            if (oldUrl) {
                var timestamps = new Date().getTime();
                var addition = ((oldUrl.indexOf('?') === -1) ? '?t=' + timestamps : '&t=' + timestamps);
                var url = requestUrlPrefix + oldUrl + addition;
                options = $.extend(options, {url: url});
            }

            if (options.callback) {
                options.success = function (res) {
                    if (res.success) {
                        var status = options.callback(res.data);
                        if (status && status !== '') {
                            $.tip.success(res.result);
                        }
                    } else {
                        $.tip.error(res.errDesc);
                    }
                }
            }
        }
        return options;
    };

    var getDataSync = function (url, method) {
        var data = {};
        request.ajax({
            url: url,
            async: false,
            type: method || 'post',
            success: function (res) {
                if (res.success) {
                    data = res.data;
                } else {
                    console.log("error: " + res);
                }
            }
        });
        return data;
    };

    //获取js文件后面的参数
    var getParam = function (jspath, parm) {
        var urlParse = jspath.split("\?");
        if (urlParse.length > 1) {
            var params = urlParse[1].split("&");
            for (var i = 0; i < params.length; i++) {
                var pr = params[i].split("=");
                if (pr[0] === parm) {
                    return pr[1];
                }
            }
        }
        return "";
    };

    var request = {};

    var init = function () {

        Array.prototype.toModel = function () {
            var obj = {};
            for (var i = 0; i < this.length; i++) {
                var item = this[i];
                if (item && item.name && item.value) {
                    obj[item.name] = item.value;
                }
            }
            return obj;
        };

        Date.prototype.format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1,                 //月份
                "d+": this.getDate(),                    //日
                "h+": this.getHours(),                   //小时
                "m+": this.getMinutes(),                 //分
                "s+": this.getSeconds(),                 //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }
            return fmt;
        };

        request.get = function (options) {
            var commonOption = getOption(options);
            if (commonOption) {
                var opt = $.extend({}, {
                    type: 'get',
                    dataType: 'json',
                    error: error
                }, commonOption);
                $.ajax(opt);
            }
        };
        request.post = function (options) {
            var commonOption = getOption(options);
            if (commonOption) {
                var opt = $.extend({}, {
                    type: 'post',
                    contentType: 'application/x-www-form-urlencoded',
                    dataType: 'json',
                    error: error
                }, commonOption);
                $.ajax(opt);
            }
        };
        request.ajax = function (options) {
            var commonOption = getOption(options);
            if (commonOption) {
                var opt = $.extend({}, {
                    dataType: 'json',
                    error: error
                }, commonOption);
                $.ajax(opt);
            }
        };

        $.loadTemplate = function (options) {
            var html = '';
            $.ajax({
                url: options.url,
                method: 'get',
                async: false,
                success: function (res) {
                    html = res;
                    if (options.data) {
                        $.each(options.data, function (k, v) {
                            html = html.replace(eval('/{{' + k + '}}/gm'), v);
                        });
                    }
                    html = html.replace(/{{.*}}/gm, '');
                }
            });
            return html;
        };

        $.tip = {
            success: function (msg) {
                alert(msg);
            },
            error: function (msg) {
                alert(msg);
            },
            warning: function (msg) {
                alert(msg);
            }
        };

        request.getDataSync = getDataSync;
        request.getParam = getParam;

        window.request = request;
        window.storage = {
            get: function (key) {
                var v = localStorage.getItem(key);
                try {
                    v = JSON.parse(v)
                } catch (e) {
                }
                return v;
            },
            set: function (key, value) {
                var v = value;
                if (typeof value === 'object') {
                    v = JSON.stringify(value);
                }
                localStorage.setItem(key, v);
            },
            remove: function (key) {
                localStorage.removeItem(key);
            }
        };
        window.moduleHeight = window.top.innerHeight - 60;
        window.requestUrlPrefix = requestUrlPrefix;

    }();

}(jQuery);